﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Data;
using Wassrile.Operation.DataClasses;

namespace Wassrile.Searchs
{
    public class SearchFavCount : SingleSearchData
    {
        public SearchFavCount()
        {
            FavSearchMethod = SearchFavCount.FavSearchMethods.Upper;
            ArgumentValue = 0;
        }

        public override string Identification
        {
            get { return "KRILE_WASSRILE_FAVC_SD"; }
        }

        public enum FavSearchMethods
        {
            Upper = 0,
            Lower,
            Just
        }

        public FavSearchMethods FavSearchMethod { get; set; }

        public int ArgumentValue { get; set; }

        protected override bool IsMatchedInner(Status status)
        {
            var ws = status as WStatus;
            if (ws == null) return false;
            var favcount = ws.FavoredUsers != null ? ws.FavoredUsers.Length : 0;
            switch (FavSearchMethod)
            {
                case FavSearchMethods.Just:
                    return favcount == ArgumentValue;
                case FavSearchMethods.Upper:
                    return favcount >= ArgumentValue;
                case FavSearchMethods.Lower:
                    return favcount <= ArgumentValue;
                default:
                    return false;
            }
        }

        public override Krile.Kernel.Control.SDataArgSetUI GetArgSetUI()
        {
            var asu = new Controls.FavArgSetUI(FavSearchMethod, ArgumentValue);
            asu.OnAccepted += new Action<FavSearchMethods, int>(asu_OnAccepted);
            return asu;
        }

        void asu_OnAccepted(SearchFavCount.FavSearchMethods arg1, int arg2)
        {
            this.FavSearchMethod = arg1;
            this.ArgumentValue = arg2;
        }

        public override string Description
        {
            get
            {
                StringBuilder ret = new StringBuilder(10);
                ret.Append("イイネの個数が ");
                ret.Append(ArgumentValue.ToString());
                ret.Append("個");
                switch (FavSearchMethod)
                {
                    case FavSearchMethods.Lower:
                        ret.Append("以下");
                        break;
                    case FavSearchMethods.Upper:
                        ret.Append("以上");
                        break;
                }
                ret.Append("のステータスを抽出します。");
                return ret.ToString();
            }
        }

        public override string Name
        {
            get { return "Wassrイイネ個数検索"; }
        }

        public override object Clone()
        {
            return new SearchFavCount() { ArgumentValue = ArgumentValue, FavSearchMethod = FavSearchMethod, Exclude = Exclude };
        }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            int nsResult;
            if (int.TryParse(reader.GetAttribute("Method"), out nsResult))
                FavSearchMethod = (FavSearchMethods)nsResult;
            reader.ReadStartElement();
            int vlResult;
            if (int.TryParse(reader.ReadElementString("Value"), out vlResult))
                ArgumentValue = vlResult;
            base.ReadXml(reader);
        }

        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Method", ((int)FavSearchMethod).ToString());
            writer.WriteElementString("Value", ArgumentValue.ToString());
            base.WriteXml(writer);
        }

        public override void AcceptArgument(object[] arguments)
        {
            if (CheckTypeof(arguments, typeof(long)))
            {
                this.ArgumentValue = (int)arguments[0];
                this.FavSearchMethod = FavSearchMethods.Upper;
            }
            else if (CheckTypeof(arguments, typeof(long), typeof(EnumValue)))
            {
                this.ArgumentValue = (int)arguments[0];
                this.FavSearchMethod = ((EnumValue)arguments[1]).AssertConvert<FavSearchMethods>();
            }
            else
            {
                ThrowOverloadTypeError(new[] { typeof(long) }, new[] { typeof(long), typeof(EnumValue) });
            }
        }

        public override string Class
        {
            get { return "Wassr"; }
        }

        public override string Method
        {
            get { return "FavCounts"; }
        }

        protected override string WriteArguments()
        {
            return this.ArgumentValue.ToString() + ", " + this.FavSearchMethod;
        }
    }
}
